﻿using System;
using System.Net;
using System.Xml;

namespace ServiceHelper
{
    public class WebServiceHelper
    {
        public static String GetResponseString(HttpWebResponse hwr)
        {
            // Get Response Result
            string resResult = NetworkUtils.ReplaceHTMLSyntax(NetworkUtils.GetResponse(hwr));

            // Remove namespace
            return XmlUtils.RemoveAllXmlNamespace(resResult);
        }

        public static bool CheckAuthentication(String serviceUrl, String username, String password)
        {
            HttpWebResponse tmp = NetworkUtils.GetResponse(serviceUrl, string.Format("username={0}&password={1}", username, password), true);
            if (tmp == null)
                return false;

            // Get Response Result
            string resResult = GetResponseString(tmp);

            try
            {
                // Now parse xml
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(resResult);

                XmlNode node = xml.SelectSingleNode("//boolean");
                return Boolean.Parse(node.InnerText.Trim());
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Check plate.
        /// Return null if connection fail!
        /// Return Empty if it is "Ve ngay".
        /// </summary>
        /// <param name="serviceUrl"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="plateNumber"></param>
        /// <returns></returns>
        public static TicketType? CheckPlate(String serviceUrl, String username, String password, String plateNumber)
        {
            HttpWebResponse tmp = NetworkUtils.GetResponse(serviceUrl, string.Format("username={0}&password={1}&motorbikePlate={2}", username, password, plateNumber), true);
            if (tmp == null)
                return null;

            // Get Response Result
            string resResult = GetResponseString(tmp);

            try
            {
                // Now parse xml
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(resResult);

                XmlNode node = xml.SelectSingleNode("//string");

                String ticketType = node.InnerText.Trim();
                if (String.IsNullOrEmpty(ticketType))
                    return TicketType.VE_NGAY;

                if ("Vé ngày".Equals(ticketType))
                    return TicketType.VE_NGAY;

                if ("Vé tháng".Equals(ticketType))
                    return TicketType.VE_THANG;

                if ("Vé Giáo Viên".Equals(ticketType))
                    return TicketType.VE_GIAOVIEN;

                // Nothing matched
                return null;
            }
            catch
            {
                return null;
            }
        }

        public static MotorbikeType? CheckMotorType(String serviceUrl, String username, String password, String plateNumber)
        {
            HttpWebResponse tmp = NetworkUtils.GetResponse(serviceUrl, string.Format("username={0}&password={1}&motorbikePlate={2}", username, password, plateNumber), true);
            if (tmp == null)
                return null;

            // Get Response Result
            string resResult = GetResponseString(tmp);
            try
            {
                // Now parse xml
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(resResult);

                XmlNode node = xml.SelectSingleNode("//string");

                String ticketType = node.InnerText.Trim();
                if (String.IsNullOrEmpty(ticketType))
                    return null;

                if ("Xe số".Equals(ticketType))
                    return MotorbikeType.XE_SO;

                if ("Xe ga".Equals(ticketType))
                    return MotorbikeType.XE_GA;

                // Nothing matched
                return null;
            }
            catch
            {
                return null;
            }
        }
    }
}